package com.unknow.first.app.version.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unknow.first.app.version.AppVersionTypeEnum;
import com.unknow.first.app.version.entity.AppVersion;
import com.unknow.first.app.version.mapper.AppVersionMapper;
import com.unknow.first.app.version.service.AppVersionService;
import com.unknow.first.app.version.vo.AppVersionParamVO;
import com.unknow.first.app.version.vo.AppVersionResultVO;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import org.cloud.exception.BusinessException;
import org.cloud.utils.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    //    @Autowired
//    private UserService userService;
    @Override
    public int insertSelective(AppVersion record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(AppVersion record) {
        return baseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Map<String, AppVersionResultVO> getAndroidAndIos(String appName) {
        HashMap<String, AppVersionResultVO> rs = new HashMap<>();
        //获取最新安卓版本
        List<AppVersion> versionList = getBaseMapper().selectNewVersion(AppVersionTypeEnum.ANDROID.getVersionType(), appName);
        if (CollectionUtil.single().isNotEmpty(versionList)) {
            for (AppVersion appVersion : versionList) {
                if (appVersion.getIsBeta() != AppVersionStatusEnum.NORMAL.getStatus()) {
                    AppVersionResultVO vo = new AppVersionResultVO();
                    BeanUtils.copyProperties(appVersion, vo);
                    rs.put(AppVersionTypeEnum.ANDROID.getTypeDesc(), vo);
                    break;
                }
            }
        }
        //获取最新ios版本
        List<AppVersion> iosVersionList = getBaseMapper().selectNewVersion(AppVersionTypeEnum.IOS.getVersionType(), appName);
        if (CollectionUtil.single().isNotEmpty(iosVersionList)) {
            for (AppVersion appVersion : iosVersionList) {
                if (appVersion.getIsBeta() != AppVersionStatusEnum.NORMAL.getStatus()) {
                    AppVersionResultVO vo = new AppVersionResultVO();
                    BeanUtils.copyProperties(appVersion, vo);
                    rs.put(AppVersionTypeEnum.IOS.getTypeDesc(), vo);
                    break;
                }
            }
        }
        return rs;
    }

    @Override
    public AppVersionResultVO isNewVersion(String version, int appType, Long userId, String appName) {
        version = version.intern();
        // todo 这里暂时配置成框架用户的ID
        //获取最新版本
        List<AppVersion> versionList = lambdaQuery().eq(AppVersion::getType, appType).eq(AppVersion::getAppName, appName).list();
        if (CollectionUtil.single().isNotEmpty(versionList)) {
            for (AppVersion appVersion : versionList) {
                //判断版本是否一致
                if (!version.equalsIgnoreCase(appVersion.getVersion())) {
                    //非内测版本 或者 当前用户为内测用户 推送给用户更新
                    if (appVersion.getIsBeta() != AppVersionStatusEnum.NORMAL.getStatus() || (userId != null && !StrUtil.isEmpty(
                        appVersion.getBetaUserId()) && (appVersion.getBetaUserId().contains(userId + "&") || appVersion.getBetaUserId()
                        .contains("&" + userId)))) {
                        AppVersionResultVO resultVO = new AppVersionResultVO();
                        BeanUtils.copyProperties(appVersion, resultVO);
                        return resultVO;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public void addVersion(AppVersionParamVO vo) throws BusinessException {
        if (vo.getType() != AppVersionTypeEnum.ANDROID.getVersionType() && vo.getType() != AppVersionTypeEnum.IOS.getVersionType()) {
            throw new BusinessException("类型不合法");
        }
        if (vo.getIsNew() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getIsNew() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("是否是最新版本不合法");
        }
        if (vo.getStatus() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getStatus() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("状态不合法");
        }
        if (vo.getIsBeta() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getIsBeta() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("状态不合法");
        }
        AppVersion version = new AppVersion();
        BeanUtils.copyProperties(vo, version);
        Date date = new Date();
        version.setCreateTime(date);
        if (getBaseMapper().insertSelective(version) > 0) {
            return;
        }
        throw new BusinessException("添加失败");
    }

    @Override
    public void updateVersion(AppVersionParamVO vo) throws BusinessException {
        if (vo.getId() <= 0) {
            throw new BusinessException("id不合法");
        }
        AppVersion appVersion = getBaseMapper().selectById(vo.getId());
        if (appVersion == null) {
            throw new BusinessException("版本不存在");
        }
        //如果要设置当前版本为非最新 ， 判断是否设置完就没有最新了， 是就拒绝当前修改
        if (appVersion.getIsNew() == AppVersionStatusEnum.NORMAL.getStatus()
            && vo.getIsNew() == AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            //获取当前类型有几个最新版本
            List<AppVersion> newVersion = getBaseMapper().selectNewVersion(appVersion.getType(), appVersion.getAppName());
            //只有一个，不允许关闭
            if (newVersion == null || newVersion.size() <= 1) {
                throw new BusinessException("未修改任何内容");
            }
        }
        //类型不允许修改
        if (vo.getType() != null) {
            vo.setType(null);
        }
        if (vo.getIsNew() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getIsNew() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("是否是最新版本不合法");
        }
        if (vo.getStatus() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getStatus() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("状态不合法");
        }
        if (vo.getIsBeta() != AppVersionStatusEnum.NORMAL.getStatus() && vo.getIsBeta() != AppVersionStatusEnum.FORBIDDEN.getStatus()) {
            throw new BusinessException("状态不合法");
        }
        AppVersion version = new AppVersion();
        BeanUtils.copyProperties(vo, version);
        Date date = new Date();
        version.setUpdateTime(date);
        if (getBaseMapper().updateByPrimaryKeySelective(version) > 0) {
            return;
        }
        throw new BusinessException("未修改任何内容");
    }

    /**
     * 通用的状态枚举
     */
    @Getter
    public enum AppVersionStatusEnum {
        /**
         * 正常
         */
        NORMAL(1),
        /**
         * 禁止
         */
        FORBIDDEN(0),
        ;
        private final int status;

        AppVersionStatusEnum(int status) {
            this.status = status;
        }

    }
}
